home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 March / EnigmA AMIGA RUN 05 (1996)(G.R. Edizioni)(IT)[!][issue 1996-03][Skylink CD IV].iso / earcd / program / ixemlsrc.lha / ixemul / stdlib / execve.c < prev    next >
C/C++ Source or Header  |  1995-12-23  |  18KB  |  642 lines

  1. /*
  2.  *  This file is part of ixemul.library for the Amiga.
  3.  *  Copyright (C) 1991, 1992  Markus M. Wild
  4.  *
  5.  *  This library is free software; you can redistribute it and/or
  6.  *  modify it under the terms of the GNU Library General Public
  7.  *  License as published by the Free Software Foundation; either
  8.  *  version 2 of the License, or (at your option) any later version.
  9.  *
  10.  *  This library is distributed in the hope that it will be useful,
  11.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13.  *  Library General Public License for more details.
  14.  *
  15.  *  You should have received a copy of the GNU Library General Public
  16.  *  License along with this library; if not, write to the Free
  17.  *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  *
  19.  */
  20.  
  21. #define KERNEL
  22. #include <string.h>
  23. #include "ixemul.h"
  24. #include "kprintf.h"
  25. #include <hardware/intbits.h>
  26. #include <ctype.h>
  27. #include <sys/wait.h>
  28. #include <stdio.h>
  29.  
  30. #include <sys/exec.h>
  31.  
  32. #include "atexit.h"
  33. #define __atexit (u.u_atexit)
  34.  
  35. #define JMP_MAGIC_16(code) ((code[0] & 0xffff0000) == 0x4efa0000)
  36. #define JMP_MAGIC_32(code) ((code[0] & 0xffff0000) == 0x4efb0000)
  37. #define JRA_MAGIC_16(code) ((code[0] & 0xffff0000) == 0x60000000)
  38.  
  39. #define MAGIC_16(code) \
  40.   ((JMP_MAGIC_16 (code) || JRA_MAGIC_16 (code)) && \
  41.    (code[1] & 0xffff) == OMAGIC)
  42.  
  43. #define MAGIC_32(code) \
  44.   (JMP_MAGIC_32 (code) && (code[2] & 0xffff) == OMAGIC)
  45.  
  46. static int compatible_startup (void *code, int argc, char **argv);
  47. static char *quote (char *orig);
  48. static void volatile on_real_stack (BPTR *segs, char **argv, char **environ, int omask);
  49.  
  50. BPTR dup2_BPTR (int);
  51. void readargs_kludge (BPTR);
  52.  
  53. int
  54. execve (const char *path, char * const *argv, char * const *environ)
  55. {
  56.   BPTR *segs;
  57.   u_int omask, err;
  58.   char *extra_args = 0;
  59.  
  60.   KPRINTF (("execve (%s,...)\n", path));
  61.   KPRINTF_ARGV ("argv", argv);
  62.   KPRINTF_ARGV ("environ", environ);
  63.  
  64.   omask = syscall (SYS_sigsetmask, ~0);
  65.  
  66.   segs = __load_seg ((char *)path, &extra_args);
  67.  
  68.   if (segs && segs != (BPTR *)-2)
  69.     {
  70.       /* Now it gets somewhat nasty... since I have to revert to the `real'
  71.          stack (since the parent will want its sp back ;-)), I have to save
  72.          the values of this stack frame into registers, or I'll never be
  73.          able to access them afterwards again.. */
  74.       register BPTR *_segs asm ("d2");
  75.       register char **_argv asm ("d3");
  76.       register char **_environ asm ("d4");
  77.  
  78.       /* if we got extra arguments, split them into a 2 el argument vector, and join
  79.        * `argv' */
  80.       if (extra_args && *extra_args)
  81.     {
  82.       char **ap;
  83.           char **nargv;
  84.           int size;
  85.  
  86.           for (size = 0, ap = (char **)argv; *ap; size++, ap++) ;
  87.           nargv = (char **) syscall (SYS_malloc, (size + 4) * 4);
  88.           ap = nargv;
  89.           *ap++ = *argv++;    /* keep the program name */
  90.           *ap++ = extra_args;
  91.           *ap = index (extra_args, ' ');
  92.           if (*ap)
  93.             {
  94.               **ap = 0;
  95.               ++*ap;
  96.               ++ap;
  97.          }
  98.           while ((*ap++ = *argv++)) ;
  99.           argv = (char * const *)nargv;
  100.     }
  101.  
  102.       _segs = segs;
  103.       _argv = (char **)argv;
  104.       _environ = (char **)environ;
  105.  
  106.       KPRINTF (("execve: about to call on_real_stack ()\n"));
  107.       if (u.p_vfork_msg)
  108.         {
  109.           set_sp ((u_int) u.u_save_sp);
  110.           /* fool the optimizer... */
  111.           asm volatile ("" : "=g" (_segs), "=g" (_argv), "=g" (_environ) : "0" (_segs), "1" (_argv), "2" (_environ));
  112.           KPRINTF (("execve () restored native sp\n"));
  113.     }
  114.       on_real_stack (_segs, _argv, _environ, omask);
  115.       /* never reached */
  116.     }
  117.  
  118.   err = ENOENT;
  119.  
  120.   syscall (SYS_sigsetmask, omask);
  121.  
  122.   errno = err;
  123.   KPRINTF (("&errno = %lx, errno = %ld\n", &errno, errno));
  124.   return -1;
  125. }
  126.  
  127.  
  128. char **
  129. dupvec (char **vec)
  130. {
  131.   int n;
  132.   char **vp;
  133.   char **res;
  134.   static char *empty[] = { NULL };
  135.   
  136.   if (! vec)
  137.     return empty;
  138.  
  139.   for (n = 0, vp = vec; *vp; n++, vp++) ;
  140.  
  141.   /* contrary to `real' vfork(), malloc() works in the child on its own
  142.      data, that is it won't clobber anything in the parent  */
  143.   
  144.   res = (char **) syscall (SYS_malloc, (n + 1) * 4);
  145.   if (res)
  146.     {
  147.       for (vp = res; n-- > 0; vp++, vec++)
  148.         *vp = (char *) syscall (SYS_strdup, *vec);
  149.  
  150.       *vp = 0;
  151.     }
  152.  
  153.   return res;
  154. }
  155.  
  156. static void volatile 
  157. on_real_stack (BPTR *segs, char **argv, char **environ, int omask)
  158. {
  159.   int private_startup;
  160.   u_int *code;
  161.   int (*entry) (struct ixemul_base *, int, char **, char **) = NULL;
  162.   struct exec *hdr = NULL;
  163.   int f;
  164.   jmp_buf old_exit;
  165.   u_int old_a4 = 0;
  166.  
  167.   KPRINTF (("entered on_real_stack()\n"));
  168.   /* first make sure that we're later passing on `safe' data to our child, ie.
  169.      copy it from wherever the data is currently stored into child malloc space */
  170.   vfork_own_malloc ();
  171.   if (environ)
  172.     *u.u_environ = dupvec(environ);
  173.   environ = *u.u_environ;
  174.   argv = dupvec(argv);
  175.   KPRINTF_ARGV ("copy of argv", argv);
  176.     
  177.   code = BTOCPTR (*segs);
  178.   code ++;    /* code starts at offset 4 */
  179.   
  180.   /* Check whether this program has our magic header.  See crt0.c for details. */
  181.  
  182.   if (MAGIC_16 (code))
  183.     {
  184.       private_startup = 1;
  185.       hdr = (struct exec *) &code[1];
  186.     }
  187.   else if (MAGIC_32 (code))
  188.     {
  189.       private_startup = 1;
  190.       hdr = (struct exec *) &code[2];
  191.     }
  192.   else
  193.     {
  194.       private_startup = 0;
  195.     }
  196.  
  197.   KPRINTF (("magic header %s\n", private_startup ? "found" : "NOT found"));
  198.   KPRINTF (("code[0] = %lx; code[1] = %lx; code[2] = %lx\n", code[0], code[1], code[2]));
  199.  
  200. #if 0
  201.   {
  202.     char **cp;
  203.     KPRINTF (("execve ["));
  204.     for (cp = argv; *cp; cp++) KPRINTF (("%s%s", *cp, cp[1] ? ", " : "], ["));
  205.     for (cp = environ; *cp; cp++) KPRINTF (("%s%s", *cp, cp[1] ? ", " : "]\n"));
  206.   }
  207. #endif
  208.  
  209.   if (private_startup)
  210.     {
  211.       entry = (void *) hdr->a_entry;
  212.       
  213.       if (! entry) private_startup = 0;
  214.     }
  215.       
  216.   /* okay, get ready to turn us into a new process, as much as
  217.      we can actually.. */
  218.  
  219.   /* close all files with the close-on-exec flag set */
  220.   for (f = 0; f < NOFILE; f++)
  221.     {
  222.       if (u.u_ofile[f] && (u.u_pofile[f] & UF_EXCLOSE))
  223.       syscall (SYS_close, f);
  224.     }
  225.  
  226.   /* BIG question what to do with registered atexit() handlers before
  227.      an exec.. Unix for sure does nothing, since the process space is
  228.      physically written over. In the AmigaDOS I could (!) imagine
  229.      cases where calling some atexit() handlers (mostly in the case
  230.      of destructors for C++ objects) would result in erronous
  231.      behaving of the program. However, since atexit() handlers also
  232.      serve to get rid of acquired Amiga resources, I morally feel
  233.      obliged to call the handlers.. lets see if this results in
  234.      incompatibilities with programs that expect Unix behavior. (Note
  235.      that I don't call exit() after exeve() returns, I call _exit(),
  236.      and _exit() does not walk the atexit() list).
  237.  
  238.      There is one special case that I catch here, this is stdio. No
  239.      Unix program would ever expect stdio buffers to be flushed by
  240.      an execve() call. So since stdio is in the library I know the
  241.      address of the handler to skip ;-)) */
  242.      
  243.   while (__atexit)
  244.     {
  245.       while (__atexit->ind --)
  246.     {
  247.       /* this is the stdio function to flush all buffers */
  248.       extern void _cleanup();
  249.     
  250.       if (__atexit->fns[__atexit->ind] != _cleanup)
  251.         {
  252.           if (u.u_a4)
  253.             asm volatile ("movel %0, a4" : : "g" (u.u_a4));
  254.           __atexit->fns[__atexit->ind] ();
  255.         }
  256.     }
  257.       __atexit = __atexit->next;
  258.     }
  259.  
  260.   /* `ignored signals remain ignored across an execve, but
  261.       signals that are caught are reset to their default values.
  262.       Blocked signals remain blocked regardless of changes to
  263.       the signal action. The signal stack is reset to be
  264.       undefined'. */
  265.  
  266.   u.u_sigonstack = 0;    /* default = on normal stack */
  267.   u.u_sigintr    = 0;    /* default = don't interrupt syscalls */
  268.   u.p_sigcatch   = 0;    /* no signals caught by user -> SIG_DFL */
  269.   for (f = 0; f < NSIG; f++)  /* reset handlers to SIG_DFL, except for SIG_IGN */
  270.     if (u.u_signal[f] != SIG_IGN)
  271.       u.u_signal[f] = SIG_DFL;
  272.  
  273.   /* what happens when we execute execve() from a signal handler
  274.      that executes on the signal stack? Better don't do this... */
  275.  
  276.   /* deinstall our sigwinch input-handler */
  277.   ix_remove_sigwinch ();
  278.           
  279.   /* save the original exit-jmpbuf, as ix_exec_entry() will destroy
  280.    * it later */
  281.   bcopy (u.u_jmp_buf, old_exit, sizeof (old_exit));
  282.   if (u.p_flag & SFREEA4)
  283.     {
  284.       old_a4 = u.u_a4;
  285.       u.p_flag &= ~SFREEA4;
  286.     }
  287.           
  288.   /* count the arguments */
  289.   for (f = 0; argv[f]; f++) ;
  290.   KPRINTF (("found %ld args\n", f));
  291.  
  292.   KPRINTF (("execve() having parent resume\n"));
  293.   if (u.p_vfork_msg)
  294.     {
  295.       /* make the parent runable again */
  296.       ReplyMsg ((struct Message *) u.p_vfork_msg);
  297.       u.p_vfork_msg = 0;
  298.     }
  299.  
  300.   KPRINTF (("execve() calling entry()\n"));
  301.   {
  302.     char *orig, **name;
  303.     struct Process *me = (struct Process *) FindTask (0);
  304.     struct CommandLineInterface *CLI = BTOCPTR (me->pr_CLI);
  305.     char *bcpl_argv0;
  306.     
  307.     bcpl_argv0 = alloca (strlen (argv[0]) + 4);
  308.     bcpl_argv0 = LONG_ALIGN (bcpl_argv0);
  309.     
  310.     if (CLI)
  311.       {
  312.     name = (char **) & CLI->cli_CommandName;
  313.     orig = *name;
  314.     bcpl_argv0[0] = strlen (argv[0]);
  315.     bcopy (argv[0], &bcpl_argv0[1], bcpl_argv0[0] + 1);
  316.     *name = (char *) CTOBPTR (bcpl_argv0);
  317.       }
  318.     else
  319.       {
  320.     name = (char **) & me->pr_Task.tc_Node.ln_Name;
  321.     orig = *name;
  322.     *name = argv[0];
  323.       }
  324.  
  325.     u.u_oldmask = omask;
  326.     if (private_startup)
  327.       entry (ixemulbase, f, argv, environ);
  328.     else
  329.       {
  330.     /*  Disable ctrl-C handling, otherwise it would be possible that
  331.      *  this process would be killed, while the child was still running.
  332.      *  Not sure at all if this is sufficient, though.
  333.      */
  334.         omask = syscall(SYS_sigsetmask, ~0);
  335.         compatible_startup (code, f, argv);
  336.         syscall(SYS_sigsetmask, omask);
  337.       }
  338.  
  339.     *name = orig;
  340.   }
  341.  
  342.   __free_seg (segs);
  343.  
  344.   if (old_a4)
  345.     {
  346.       u.u_a4 = old_a4; 
  347.       u.p_flag |= SFREEA4;
  348.     }
  349.   KPRINTF (("old program doing _exit(%ld)\n", f));
  350.   /* and fake an _exit */
  351.   _longjmp (old_exit, 1);
  352. }  
  353.  
  354.  
  355. /* some rather rude support to start programs that don't have a struct exec
  356.  * information at the beginning.
  357.  * 1.3 NOTE: This will only start plain C programs, nothing that smells like
  358.  *           BCPL. Limited support for ReadArgs() style parsing is here, but not
  359.  *         everything is set up that would have to be set up for BCPL programs
  360.  *         to feel at home. Also don't use Exit() in those programs, it wouldn't
  361.  *         find what it expects on the stack....
  362.  */
  363. static int
  364. compatible_startup (void *code, int argc, char **argv)
  365. {
  366.   char *al;
  367.   int max, res;
  368.   u_int oldsigalloc;
  369.   struct Process *me = (struct Process *) FindTask (0);
  370.   
  371.   KPRINTF (("entered compatible_startup()\n"));
  372.   KPRINTF (("argc = %ld\n", argc));
  373.   KPRINTF_ARGV ("argv", argv);
  374.  
  375.   /* ignore the command name ;-) */
  376.   argv++;
  377.  
  378.   max = 1024;
  379.   al = (char *) kmalloc (max);
  380.   res = -1;
  381.   if (al)
  382.     {
  383.       char *cp;
  384.       BPTR old_cis, old_cos, old_ces;
  385.       BPTR dup_cis, dup_cos, dup_ces;
  386.       void *old_trapdata, *old_trapcode;
  387.       int old_flags;
  388.       void *old_launch, *old_switch;
  389.       struct file *f;
  390.  
  391.       for (cp = al; *argv; )
  392.         {
  393.       char *newel = quote (*argv);
  394.           int elsize = strlen (newel ? newel : *argv) + 2;
  395.       KPRINTF (("arg [%s] quoted as [%s]\n", *argv, newel ? newel : *argv));
  396.           
  397.           if (cp + elsize >= al + max)
  398.             {
  399.           char *nal;
  400.               max <<= 1;
  401.               nal = (char *) krealloc (al, max);
  402.               if (! nal) break;
  403.               cp = nal + (cp-al);
  404.               al = nal;
  405.         }
  406.  
  407.       strcpy (cp, newel ? newel : *argv);
  408.       cp += elsize - 2;
  409.       *cp++ = ' ';
  410.       *cp = 0;
  411.       if (newel) kfree (newel);
  412.       ++argv;
  413.         }
  414.       
  415.       /* BCPL weirdness ... */
  416.       *cp++ = '\n';
  417.       *cp = 0;
  418.  
  419.       KPRINTF (("BCPL cmd line = [%s]\n", al));
  420.  
  421.       /* problem with RunCommand: the allocated signal mask is not reset
  422.      for the new process, thus if several RunCommands are nested, a
  423.      late started process might run out of signals. This behavior makes
  424.      no sense, since the starting process is *suspended* while the `child'
  425.      is running, thus it doesn't need its signals in the meantime ! */
  426.  
  427.       oldsigalloc = me->pr_Task.tc_SigAlloc & 0xffff0000;    /* hacky...*/
  428.       me->pr_Task.tc_SigAlloc &= 0xffff;
  429.  
  430.       /* cleanup as much of ixemul.library as possible, so that the started
  431.          process can take over */
  432.       old_flags             = me->pr_Task.tc_Flags;
  433.       me->pr_Task.tc_Flags  = u.u_otask_flags;
  434.       old_launch            = me->pr_Task.tc_Launch;
  435.       me->pr_Task.tc_Launch = u.u_olaunch; /* restoring this disables our signals */
  436.       old_switch            = me->pr_Task.tc_Switch;
  437.       me->pr_Task.tc_Switch = u.u_oswitch;
  438.       RemIntServer (INTB_VERTB, & u.u_itimerint);
  439.  
  440.       /* limited support (part 2 ;-)) for I/O redirection on old programs
  441.          If we're redirecting to a plain file, don't go thru a IXPIPE, 
  442.          temporarily use our DOS files in that case. Any other file type
  443.          is routed thru an IXPIPE though. */
  444.       
  445.       if ((f = u.u_ofile[0]) && f->f_type == DTYPE_FILE)
  446.         {
  447.           dup_cis = 0;
  448.           old_cis = SelectInput (CTOBPTR (f->f_fh));
  449.           readargs_kludge (CTOBPTR (f->f_fh));
  450.         }
  451.       else
  452.         {
  453.           if (!f)
  454.             {
  455.               int fd = syscall(SYS_open, "/dev/null", 0);
  456.               dup_cis = dup2_BPTR (fd);
  457.               syscall(SYS_close, fd);
  458.             }
  459.           else
  460.             dup_cis = dup2_BPTR (0);
  461.           old_cis = 0;
  462.           if (dup_cis)
  463.             {
  464.               old_cis = SelectInput (dup_cis);
  465.               readargs_kludge (dup_cis);
  466.             }
  467.         }
  468.       if ((f = u.u_ofile[1]) && f->f_type == DTYPE_FILE)
  469.         {
  470.           dup_cos = 0;
  471.           old_cos = SelectOutput (CTOBPTR (f->f_fh));
  472.         }
  473.       else
  474.         {
  475.           if (!f)
  476.             {
  477.               int fd = syscall(SYS_open, "/dev/null", 1);
  478.               dup_cos = dup2_BPTR (fd);
  479.               syscall(SYS_close, fd);
  480.             }
  481.           else
  482.             dup_cos = dup2_BPTR (1);
  483.           old_cos = 0;
  484.           if (dup_cos)
  485.             old_cos = SelectOutput (dup_cos);
  486.         }
  487.       old_ces = me->pr_CES;
  488.       if ((f = u.u_ofile[2]) && f->f_type == DTYPE_FILE)
  489.         {
  490.           dup_ces = 0;
  491.           me->pr_CES = CTOBPTR (f->f_fh);
  492.         }
  493.           else
  494.         {
  495.           if (!f)
  496.             {
  497.               int fd = syscall(SYS_open, "/dev/null", 2);
  498.               dup_ces = dup2_BPTR (fd);
  499.               syscall(SYS_close, fd);
  500.             }
  501.           else
  502.             dup_ces = dup2_BPTR (2);
  503.           me->pr_CES = dup_ces ? : old_ces;
  504.         }
  505.  
  506.       /* BEWARE that after this reset no library functions can be
  507.          called any longer until the moment where trapdata is 
  508.          reinstalled !! */
  509.       old_trapdata = me->pr_Task.tc_TrapData;
  510.       me->pr_Task.tc_TrapData = u.u_otrap_data;
  511.       old_trapcode = me->pr_Task.tc_TrapCode;
  512.       me->pr_Task.tc_TrapCode = u.u_otrap_code;
  513.  
  514.       {
  515.     struct CommandLineInterface *CLI = BTOCPTR (me->pr_CLI);
  516.     u_int stack_size = CLI ? CLI->cli_DefaultStack * 4 : me->pr_StackSize;
  517.  
  518.     /* perhaps someone really uses so small stacks......... */
  519.     /* if (stack_size <= 4096) stack_size = 250000; */
  520.  
  521.     /* the above approach has too many incompatibilities, sigh.
  522.  
  523.        Note: The use of RunCommand() here means, that we *waste* the
  524.              entire stack space allocated for this process! If someone
  525.              comes up with a clever trick (probably involving StackSwap ())
  526.              where the stack of this process can be freed before calling
  527.              RunCommand (), lots of users with memory problems would be
  528.              thankful! */
  529.  
  530.     res = RunCommand (CTOBPTR (code) - 1, stack_size, al, cp - al);
  531.       }
  532.       /* reinstall enough of ixemul to be able to finish cleanly 
  533.          (the recent addition of an ix_sleep() at the end of a vfork'd
  534.           process makes it necessary to reinstall the signalling facilities!) */
  535.  
  536.       me->pr_Task.tc_TrapData = old_trapdata;
  537.       me->pr_Task.tc_TrapCode = old_trapcode;
  538.       /* have to do this, or ix_close() is not able to RemoveIntServer .. */
  539.       AddIntServer (INTB_VERTB, & u.u_itimerint);
  540.       me->pr_Task.tc_Launch = old_launch;
  541.       me->pr_Task.tc_Switch = old_switch;
  542.       me->pr_Task.tc_Flags  = old_flags;
  543.  
  544.       kfree (al);
  545.  
  546.       if (old_cis)
  547.     SelectInput (old_cis);
  548.       if (old_cos)
  549.     Flush (SelectOutput (old_cos));
  550.       me->pr_CES = old_ces;
  551.  
  552.  
  553.       if (dup_cis)
  554.         Close (dup_cis);
  555.       if (dup_cos)
  556.         Close (dup_cos);
  557.       if (dup_ces)
  558.         Close (dup_ces);
  559.  
  560.       me->pr_Task.tc_SigAlloc |= oldsigalloc;
  561.     }
  562.  
  563.   u.p_xstat = W_EXITCODE(res, 0);
  564.   return res;
  565. }
  566.  
  567. static char *
  568. quote (char *orig)
  569. {
  570.   int i;
  571.   char *new, *cp;
  572.   
  573.   i = strlen (orig);
  574.   
  575.   if (strpbrk (orig, "\"\'\\ \t\n"))
  576.     {
  577.       /* worst case, each character needs quoting plus starting and ending " */
  578.       new = (char *) kmalloc (i * 2 + 3);
  579.       if (! new) return 0;
  580.  
  581.       cp = new;
  582.       *cp++ = '"';
  583.       while (*orig)
  584.         {
  585.           if (index ("\"\\", *orig))
  586.             *cp++ = '\\';
  587.       *cp++ = *orig++;
  588.     }
  589.       *cp++ = '"';
  590.       *cp = 0;
  591.       
  592.       return new;
  593.     }
  594.   else
  595.     return 0;    /* means `just use the original string' */
  596. }   
  597.  
  598. /* try to obtain a DOS filehandle on the specified descriptor. This only
  599.    works, if the user has mounted IXPIPE: */
  600. BPTR
  601. dup2_BPTR (int fd)
  602. {
  603.   long id;
  604.   char name[20];
  605.   
  606.   id = syscall(SYS_fcntl, fd, F_EXTERNALIZE, 0);
  607.   if (id >= 0)
  608.     {
  609.       sprintf (name, "IXPIPE:%x", (unsigned int)id);
  610.       /* 0x4242 is a magic packet understood by IXPIPE: to F_INTERNALIZE id */
  611.       return Open (name, 0x4242);
  612.     }
  613.  
  614.   return 0;
  615. }
  616.  
  617.  
  618. /* the mysteries of DOS seem to never want to take an end... */
  619. void
  620. readargs_kludge (BPTR bp)
  621. {
  622.   int ch;
  623.   static const int EOS_CHAR = -1;
  624.  
  625. #if 0
  626.   /* the autodocs say this bug is fixed after v37, well, perhaps that was a
  627.      very deep wish, nevertheless unheard by dos...
  628.      Without this kludge, you have to actually press return if stdin is not
  629.      redirected...
  630.      Thanks mbs: without your shell code I would never have guessed that 
  631.                  something that weird could be possible....
  632.    */
  633.   if (ix.ix_dos_base->lib_Version <= 37)
  634. #endif
  635.     {
  636.       ch = UnGetC (bp, EOS_CHAR) ? 0 : '\n';
  637.       while ((ch != '\n') && (ch != EOS_CHAR))
  638.         ch = FGetC (bp);
  639.       Flush (bp);
  640.     }
  641. }
  642.